<template>
  <div id="app">
    <el-card shadow="always">
      <el-form :inline="true" size="medium" :model="studentSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="studentSearch.cid" placeholder="所在班级">
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="editStudent" icon="el-icon-plus">添加学生</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="studentList"
          style="width: 100%">
        <el-table-column
            label="学号"
            prop="sid"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="sname">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column
            label="住址"
            align="center"
            prop="addr">
        </el-table-column>
        <el-table-column
            label="所在班级"
            align="center"
            prop="cname">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="editStudent">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="Deletestudent(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="student">
        <el-form-item label="学生学号" :label-width="formLabelWidth">
          <el-input v-model="studentedit.sid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="studentedit.sname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生性别" :label-width="formLabelWidth">
          <el-radio v-model="studentedit.sex" label="男">男</el-radio>
          <el-radio v-model="studentedit.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="studentedit.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生住址" :label-width="formLabelWidth">
          <el-input v-model="studentedit.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生班级" :label-width="formLabelWidth" style="width: 1200px">
          <el-select v-model="studentedit.cid" placeholder="请选择学生班级" >
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveStudent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import studentApi from "@/api/student"
import classesApi from "@/api/classes"
export default {
  data() {
    return {
      studentList: [],      // 当前页的所有学生信息集合
      page:1,               // 当前页
      pageSize:2,           // 每页大小
      total:100,            // 总记录数
      studentSearch:{},
      classesList:[],
      dialogFormVisible:false,
      formLabelWidth: '150px',
      studentedit:{},
      title: '编辑学生',
      deletes:0 ,
    }
  },
  created() {
    this.findClasses()
    this.search()
  },

  methods: {
    // 1. 查询所有学生
    findStudents(){
      this.$axios.get('/student/list.do')
          .then(res => {
            console.log("数据：",res.data)
            this.studentList = res.data
          })
    },
    // 2. 进行分页查询
    async findByPage(page,pageSize){
        const {data:res} = await studentApi.findByPage(this.page,this.pageSize)
        this.studentList = res.rows
        this.total = res.total
    },
     //3.查询班级
     async findClasses(){
         const {data:res} = await classesApi.findClasses()
         this.classesList = res
     },
    //4.条件查询加分页
    async search(){
      const {data} = await studentApi.search(this.page,this.pageSize,this.studentSearch)
      this.studentList = data.rows
      this.total = data.total
    },
    // 5. 编辑学生
    editStudent(){
      this.dialogFormVisible = true
    },

    //6.保存学生信息到数据库
    async saveStudent(){
      const {data:res} = await studentApi.save(this.studentedit)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },

    async Deletestudent(row){
        const {data:res} = await studentApi.deletes(row.sid)
              if (res){
                this.$message({
                  type: 'success',
                  message: "删除成功"
                })
                this.search()
              }else {
                this.$message({
                  type: 'error',
                  message: "删除失败"
                })
              }
    },

    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      //this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      //this.findByPage(this.page,this.pageSize)
      this.search()
    }
  }
}
</script>
<style>
  .el-card{
    width: 90%;
    margin: 0 auto;
  }
  .el-pagination{
    text-align: right;
    margin-top: 15px;
  }
</style>
